package controller;

import dao.FieldDao;
import dao.OrderDao;
import dao.UserDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;
import pojo.Field;
import pojo.Order;
import pojo.OrderRespVo;
import pojo.User;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HLF
 * @date 2022/6/14 16:42
 */
@WebServlet("/orderController")
public class OrderController extends BaseController {
    /**
     * 查询预约记录列表
     */
    public List<OrderRespVo> list() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);
        UserDao userDao = sqlSession.getMapper(UserDao.class);


        List<Order> orders = orderDao.getAllOrders();
        List<OrderRespVo> orderRespVos = orders.stream().map(order -> {
            try {
                OrderRespVo orderRespVo = new OrderRespVo();
                BeanUtils.copyProperties(orderRespVo, order);
                Field field = fieldDao.getByFid(order.getFid());
                orderRespVo.setFname(field.getFname());
                orderRespVo.setPrice(field.getPrice());
                orderRespVo.setUsername(userDao.getByUid(order.getUid()).getUsername());

                return orderRespVo;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());

        sqlSession.close();
        return orderRespVos;
    }

    /**
     * 跳转到预约记录页面
     */
    protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有预约记录
        List<OrderRespVo> orders = this.list();
        req.setAttribute("orders", orders);

        //查询所有用户和所有场地(用于新增和修改的下拉列表回显)
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<Field> allFields = fieldDao.getAllFields();
        List<User> allUsers = userDao.getAllUsers();
        req.setAttribute("allFields", allFields);
        req.setAttribute("allUsers", allUsers);

        req.getRequestDispatcher("/orderManage.jsp").forward(req, resp);
    }


    /**
     * 添加预约记录
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fid = req.getParameter("fid");
        String uid = req.getParameter("uid");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        Order order = new Order(null, Integer.valueOf(fid), Integer.valueOf(uid), new Date());
        orderDao.insertOrder(order);

        //改变对应的场地的状态为已预约
        fieldDao.updateStatusByFid(Integer.valueOf(fid), 1);

        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);
    }

    /**
     * 修改预约记录
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        String fid = req.getParameter("fid");
        String uid = req.getParameter("uid");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);

        orderDao.updateOrder(new Order(Integer.valueOf(oid), Integer.valueOf(fid), Integer.valueOf(uid), new Date()));


        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);
    }

    /**
     * 根据oid获取预约记录（打开修改模态框回显数据）
     */
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        resp.setContentType("text/html;charset=UTF-8");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);

        Order qorder = orderDao.getByOid(Integer.valueOf(oid));
        req.setAttribute("qorder", qorder);

        //查询所有用户和所有场地(用于新增和修改的下拉列表回显)
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<Field> allFields = fieldDao.getAllFields();
        List<User> allUsers = userDao.getAllUsers();
        req.setAttribute("allFields", allFields);
        req.setAttribute("allUsers", allUsers);

        this.index(req, resp);
    }

    /**
     * 删除预约记录
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        //改变对应的场地的预约状态为未预约
        Order order = orderDao.getByOid(Integer.valueOf(oid));
        fieldDao.updateStatusByFid(order.getFid(), 0);

        orderDao.deleteByOid(Integer.valueOf(oid));


        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);
    }
}
